package com.yc.projects.pro4_mysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class BookDaoImpl implements BookDao{

    @Autowired
    private DataSource dataSource;

    @Override
    public List<BookBean> findBook() {
        List<BookBean> list = new ArrayList<>();
        try(Connection con = dataSource.getConnection();
            PreparedStatement pstmt = con.prepareStatement("select * from books");
        ){
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                BookBean ba = new BookBean();
                ba.setId(rs.getInt("id"));
                ba.setBookname(rs.getString("bookname"));
                ba.setBookpress(rs.getString("bookpress"));
                ba.setPressdate(rs.getString("pressdate"));
                ba.setBookauthor(rs.getString("bookauthor"));
                ba.setBookcount(rs.getInt("bookcount"));
                ba.setBookimage(rs.getString("bookimage"));
                list.add(ba);

            }

        }catch (SQLException e){
            e.printStackTrace();
            }
            return list;
    }
}
